<?php
session_start();

include_once('config.php');
include_once('MysqlDao.php');
include_once('WorkManageAction.php');
include_once('WorkManageDao.php');
include_once('kpiAction.php');
include_once('kpiDao.php');
include_once('officeAction.php');
include_once('officeDao.php');
include_once('util.php');
include_once("inc/lib/smarty/Smarty.class.php");
include_once("inc/classes/page_class.php");

class ActionControl extends MysqlDao  
{
	public $parameter;
	 
	//构造函数
	public function ActionControl()
	{
		$this->parameter = stageRouter();
	    $_SESSION['url'] = empty($_SESSION['temp']) ?  'index.php' : $_SESSION['temp'];
	    $_SESSION['temp'] = url();	
		
		if (gettype($this->parameter) == 'array') 
		{
			 $act = $this->parameter[0];
			 if($act != 'volidLogin' && $act != 'validDataCode') 
			 {
				if (empty($_SESSION['name']) || !isset($_SESSION['type'])) 
				{
					Header("Location:index.php");
				}
			 }

			 $this->con();
				  
			 if( eachClss($act) ) 
			 {
				 $this->$act();
			 } else {
				 exit(message('未找到相应处理'));
			 }
		}
	}
	
	//模板引擎
	private function smartyObject()
	{
		$template = new Smarty;
		$template->template_dir =  WEB_HOME . '/view/'.TPL_NAME;
		$template->compile_dir = WEB_HOME . '/view/cache/';
		$template->config_dir = WEB_HOME . '/view/cache/';
		$template->cache_dir = WEB_HOME . '/view/cache/';
		$template->left_delimiter = "<{";
		$template->right_delimiter = "}>";
		$template->caching = false;
		return $template;
	}
	
	//主界面
	public function main()
	{
		$t = $this->smartyObject();
		$t->display('#index.html');
	}

	// 工作管理
	public function workManage()
	{
		$act = new WorkManageAction;
		if( count( $this->parameter ) <= 1 ) {
			$act->workManage();
			return;
		} 

		$do = $this->parameter[ 1 ];
		$act->$do();
	}

	// 绩效考核
	public function kpiManage()
	{
		$act = new kpiAction;
		if( count( $this->parameter ) <= 1 ) {
			$act->kpi();
			return;
		}

		$do = $this->parameter[ 1 ];
		$act->$do();
	}

	// 行政管理
	public function officeManage()
	{
		$act = new officeAction;
		if( count( $this->parameter ) <= 1 ) {
			$act->office();
			return;
		}

		$do = $this->parameter[ 1 ];
		$act->$do();
	}
	
	//项目跟进主页面
	public function projectMenu()
	{
		if ($_SESSION['type'] == 2 || $_SESSION['departmentId'] == 1) {
		$t = $this->smartyObject();
		$id = 0;
		if(count($this->parameter) > 1){
	    	$id = intval($this->parameter[1]);
		}		
		$t->assign("type" , $id);
		$t->assign("userList" , $this->viewDepartmentUserList(1));
		$t->display("#projectMenu.html");
		}
	}
	
	//管理员主页面
	public function systemMenu()
	{
		if ($_SESSION['type'] == 0) 
			exit;

		$t = $this->smartyObject();
		$t->display("#systemMenu.html");
	}
	
	//用户列表
	public function userList()
	{
		if ($_SESSION['type'] == 0) 
			exit;

		$t = $this->smartyObject();
		$t->assign("userList" , $this->viewUserList());
		$t->assign("departmentList" ,  $this->viewDepartmentList() );
		$t->display('#userList.html');
	}
	
	//项目列表
	public function projectList()
	{
		
		if ($_SESSION['type'] == 2 || $_SESSION['departmentId'] == 1) {			
		$pageStr = '';
		if($_SESSION['type'] == 2){
			$when = '';
		 if (count($this->parameter) >= 2) {
		 	$when = 'userId ='.$this->parameter[1];
		    $pageStr = $this->parameter[1];
		 }
		   $rows = $this->viewTableCount("project",$when); 
		}else { 
		   $rows = $this->viewTableCount("privilege" ,"userId =".$_SESSION['id']);
		}
		$page = empty($_GET['page']) ? 1 : intval($_GET['page']);
		$rs = new Pages($rows , PAGESIZE , $page , $pageStr);
		
		$url = '?q=projectList/' ;
		$t = $this->smartyObject();
		$userId = '';
		if(count($this->parameter) > 1){
			$userId = intval($this->parameter[1]);
		}		
	
		$t->assign("projectList",$this->viewPorjectList($rs->start_row , $userId));
		$t->assign("pageInfo" , $rs->show_default($url));
		$t->display("#projectList.html");
		}
	}
	
	//查看详细页面
	public function details()
	{
		if ($_SESSION['type'] == 2 || $_SESSION['departmentId'] == 1) {
		$id = intval($this->parameter[1]);
	    $this->IsView($id);
		$data = $this->viewDetailsData($id ,"project");
		$filed = array_keys($data);
		$t = $this->smartyObject();
		for ($i = 1 ; $i < count($filed); $i++)
		{			
			if (gettype( $filed[$i]) == "string") {
				$t->assign($filed[$i] , $data[$filed[$i]]);
			}
		}
		$t->display("#details.html");
		}
	}
	
	//详细页面楼层
	public function detailsDataBss()
	{
		if ($_SESSION['type'] == 2 || $_SESSION['departmentId'] == 1) {
		$id = intval($this->parameter[1]);
		$this->IsView($id);
		$rows  = $this->viewTableCount("followinfo" , "child =".$id);
		$page = empty($this->parameter[2]) ? 1 : intval($this->parameter[2]);
		$rs = new Pages($rows , PAGESIZE , $page , '');
		$url = "?q=detailsDataBss/$id/";
		$t = $this->smartyObject();
		$t->assign("dataList" , $this->viewDetailsBss($id , $rs->start_row));
		$t->assign("page" , $rs->show_integer($url));
		$t->display("#detailsBss.html");	
	   }	
	}
	
	//添加跟进信息
	public function addFollowInfoValid()
	{
		if ($_SESSION['type'] == 2 || $_SESSION['departmentId'] == 1) {
		if (empty($_POST['content'])) {
			message('必填项不能为空');
			exit;
		}else {
		    $this->addFollowinfo();
		    Header("Location:?q=details/$_POST[id]");
		}
	  }
	}

	//添加新项目信息
	public function addProjectInfo()
	{
		if ($_SESSION['type'] == 2 || $_SESSION['departmentId'] == 1) {
		$t = $this->smartyObject();
		$t->assign("resourceList" , $this->viewResourceList());
		$t->display("#detailsArtilce.html");	
		}
	}
	
	//修改项目信息页面
	public function editProjectInfo(){
	 	if ($_SESSION['type'] == 2 || $_SESSION['departmentId'] == 1) {
	 	$t = $this->smartyObject();
	 	$t->assign("resourceList" , $this->viewResourceList());
	 	$id =  intval($this->parameter[1]);
	 	$list = $this->viewDetailsData($id ,"project");
	 	$fields = array_keys($list);
	 	foreach ($fields as $v)
	 	{
	 	  if (gettype($v) == "string") {
	 	  	 $t->assign($v , $list[$v]);
	 	  }	 	  
	 	}
	 	$t->display("#detailsArtilce.html");
	 	}
	 }
 
    //查看分配页
    public function viewAssign()
    {
    	if ($_SESSION['type'] != 2) exit; 
    	$t = $this->smartyObject();
    	$id =  intval($this->parameter[1]);
    	$t->assign("userList" , $this->getUserList($id));
    	$t->assign("id" , $id);
    	$t->display("#msgSet.html");    	
    }
   
    //新建群发邮件页面
    public function sendMail(){
    	if ($_SESSION['type'] == 0) exit; 
    	$t = $this->smartyObject();
    	$t->display("#sendMail.html");
    }
    
    //部门树
    public function departmentTree(){
    	if ($_SESSION['type'] == 0) exit; 
    	$t = $this->smartyObject();
		$t->assign("departmentList" , $this->viewDepartmentList());
		$t->assign("userList" , $this->viewDepartmentUserList(00));
		$t->display("#DepartmentTree.html");
    }
    
    //添加新的报备信息
    public function addReportedInfo()
    {
    	if ($_SESSION['type'] == 2 || $_SESSION['departmentId'] == 1) {
    	$t = $this->smartyObject();
		$t->assign("resourceList" , $this->viewResourceList());
		$t->display("#reportedArtilce.html");
    	}		
    }
    
    //修改报备信息页面
    public function editReportedInfo()
    {
     if ($_SESSION['type'] == 2 || $_SESSION['departmentId'] == 1) {
      $t = $this->smartyObject();
      $t->assign("resourceList" , $this->viewResourceList());
      $id =  intval($this->parameter[1]);
      $list = $this->viewDetailsData($id ,"reported");
      $fields = array_keys($list);
      foreach ($fields as $v)
      {
      	if (gettype($v) == "string") {
      		$t->assign($v , $list[$v]);
      	}
      }
      $t->display("#reportedArtilce.html");
     }
    }
    
    //报备列表
    public function reportedList()
    {
    	if ($_SESSION['type'] == 2 || $_SESSION['departmentId'] == 1) {
    	$page = empty($_GET['page']) ? 1 : intval($_GET['page']);
    	$pageStr = '';
    	$str = '';
    	$flag = 0;
    	$value = '';
    	if (!empty($_GET['id'])) {
    		$value = intval($_GET['id']);
    		$rows = $this->viewTableCount("reported" ,"userId =".$value);
    	    $flag = 1;
    	    $str = '&id='.intval($_GET['id']);
    	}elseif (!empty($_GET['title'])){
    		$value = array($_GET['typeName'],$_GET['title']);
    		$rows = $this->viewTableCount("reported" , $value[0]." like '%".urldecode($value[1])."%'");
    		$flag = 2;
    		$str = '&typeName='.$value[0].'&title='.$value[1];
    	}else {
    		$rows = $this->viewTableCount("reported" , "");    		
    	}    	
		$rs = new Pages($rows , PAGESIZE , $page , $pageStr);		
		$url = '?q=reportedList'.$str ;
		$t = $this->smartyObject();
		$t->assign("reportedList",$this->viewReportedList($rs->start_row , $flag , $value));
		$t->assign("pageInfo" , $rs->show_default($url));
		$t->display("#reportedList.html");	
     }
    }
     
    //查看工作报告主页面
    public function wordSummaryMenu()
    {
		$t = $this->smartyObject();
		$t->assign("departmentList" , $this->viewDepartmentList());
		$t->assign("userList" , $this->viewDepartmentUserList(00));
		$t->display("#wordsummaryMenu.html");
    }
    
    //查看工作报告分页
    public function wordSummaryList()
    {        	
		if ($_SESSION['purview'] == 1 || $_SESSION['type'] > 0) {			
		   if (!empty($_GET['userId'])) {
		   	$when = ' userId ='.intval($_GET['userId']);
		   	$rows = $this->viewTableCount("wordsummary" ,$when);
		   }
		   elseif (!empty($_GET['date'])) {
		   	$when = ' recordTime ="'.$_GET['date'].'"';
		   	$rows = $this->viewTableCount("wordsummary" ,$when);
		   }
		   elseif (!empty($_GET['departmentId'])) {	   	
		   	$when = 'u.departmentId = '.intval($_GET['departmentId']);		   	
		   	$rows = $this->wordSummaryCoun(intval($_GET['departmentId']));
		   }else{
		   	 $rows = $this->viewTableCount("wordsummary" ,'');
		   }  
		 
		}else {
			$rows = $this->viewTableCount("wordsummary" ,'userId ='.$_SESSION[id]);
		}		
		$page = empty($_GET['page']) ? 1 : intval($_GET['page']);
		$number = PAGESIZE;
		if (empty($_GET['browsing'])) {
			$browsing  = 'browsing=1';
			$cssClass = 'list';
		}else {
			$browsing  = 'browsing='.intval($_GET['browsing']);
			$cssClass = 'list none';
			$number = PAGESIZE * 2;
		}
		$rs = new Pages($rows , $number , $page , $browsing);		
		$url = '?q=wordSummaryList' ;
		$t = $this->smartyObject();
		$t->assign("dataList",$this->summaryListData($rs->start_row ,$when , $number));
		$t->assign("pageInfo" , $rs->show_default($url));
		$t->assign('cssClass' , $cssClass);
		$t->display("#wordsummaryList.html");
    }    

    //填写工作报告
    public function viewWordSummary()
    {
    	$t = $this->smartyObject();
    	$data = $this->viewGetDaySummary();
    	if (count($data) > 0) {
    		$t->assign("content_info" ,str_replace( "  ", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",nl2br($data['summary'])));
    		$t->assign("content" , $data['summary']);
    		$t->assign("plan_info" , str_replace( "  ", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",nl2br($data['plan'])));
    		$t->assign("plan" , $data['plan']);
    	}
    	
    	$t->display("#wordsummary.html"); 
    }
	
    //验证工作信息报告
    public function summaryValid()
    {
    	if (empty($_POST['content']) || empty($_POST['plan'])) {
    		exit(message('必填内容不能为空','index.php?q=viewWordSummary'));
    	}else{
    		$this->addWordSummary();
    		Header("Location:?q=viewWordSummary");
    	}
    }
	
    //异步增加购物车实例
    public function Shopping(){
    	if ($_SESSION['type'] == 0) exit;
    	if (count($this->parameter) >= 2) {
    		if (empty($_SESSION['shopping'])) {
    			$list = array();
    			array_push($list , intval($this->parameter[2]));
    			$_SESSION['shopping'] = serialize($list);	
    			echo 0;		
    		}else {
    			$list = unserialize($_SESSION['shopping']);
    			$id = $this->parameter[2];
    			if ($this->parameter[1] == 1) $list[$id] = $id;    	
    			else unset($list[$id]);
    		   $list = array_unique($list);
    		   $_SESSION['shopping'] = serialize($list);
    		   echo count($list);
    		}
    	}
    }    
   
    //资源部门列表
    public function resourceDepartmentList(){
    	if($_SESSION['type'] != 2) exit;
    	$id = intval($this->parameter[1]);
    	if ($id > 0) {
    	  $typeName = $id == 2 ? '资源' : '部门';
    	  $t = $this->smartyObject();
    	  $t->assign('typeName' , $typeName);
    	  $t->assign('typeid' , $id);
    	  $t->assign('dataList' , $this->viewRDList($id));
    	  $t->display('#resourceList.html');
    	}
    }
    
    //修改密码
    public function changePass(){
    	$t = $this->smartyObject();
    	$t->display('#pass.html');
    }
    
    //退出
    public function userExit(){
    	unset($_SESSION);
    	session_unset();
        session_destroy();
    	Header("Location:index.php");
    }
    
    //登录界面
    public static function login(){
       $t = self::smartyObject();	
       $t->display('#login.html');
    }
    
    //历史发送记录
    public function sendHistoryList(){
       if ($_SESSION['type'] == 0) exit; 
       $t = $this->smartyObject();
       $page = empty($_GET['page']) ? 1 : intval($_GET['page']);
       $rows = $this->viewTableCount("sendhistory" , ''); 
	   $rs = new Pages($rows , PAGESIZE , $page , '');		
	   $url = '?q=sendHistoryList' ;
	   $t = $this->smartyObject();
	   $t->assign("historyList",$this->viewSendHiStory($rs->start_row));
	   $t->assign("pageInfo" , $rs->show_default($url));
	   $t->display("#hiStoryList.html");		    	
    }
    
    //报备详细
    public function reportDetails(){
     if ($_SESSION['type'] == 2 || $_SESSION['departmentId'] == 1) {
    	$id = intval($this->parameter[1]);
    	$list =$this->viewRSDetailsData($id , 0);
    	$filed = array_keys($list);
    	$t = $this->smartyObject();
    	foreach ($filed as $value ){
    		$t->assign($value , $list[$value]);
    	}
    	$t->display('#reportDetails.html');
     }
    }
    
    //发送历史
    public function sendHistoryDetails(){
      if($_SESSION['type'] == 0) exit;
      $id = intval($this->parameter[1]);
      $list =$this->viewRSDetailsData($id , 1);
      $filed = array_keys($list);
      $t = $this->smartyObject();
      foreach ($filed as $value ){
    		$t->assign($value , $list[$value]);
     }
     $t->display('#sendHistoryDetails.html');      
    }
    
    //发送邮件处理
    public function sendMailAction(){
      	if ($_SESSION['type'] == 0) {
      		exit; 
      	}
      		
        $err = 0;
    	if (empty($_POST['title']) || empty($_POST['content'])) {
    		exit(message('必填项不能为空！'));
    	}elseif (empty($_POST['qq'])){
    		$list = $this->viewDepartmentUserList(00);
    		$qq = array();
    		foreach ($list as $key=>$value){
    			if(!empty($value['qq'])){
				 $qq[$key] = $value['qq'].'@qq.com';
				}				
    		}    		
    	}else {
    		$qq = substr($_POST['qq'] , 0 , strlen($_POST['qq']) -1);
    	    $list = explode(",",$qq);
    	    $i= $err = 0;
			$qq = array();
    	    foreach ($list as $value){
    	    	$qq[$i] = $value.'@qq.com';
    	    	$i++;
    	    }
    	}
    	
    	$this->sendSingleMail( $_POST['title'], $_POST['content'], $qq );
    }
    
    // 发送邮件
    private function sendSingleMail($subject, $content, $mail_addr_list) {
    	require(WEB_HOME."/inc/lib/phpmailer/class.phpmailer.php"); 
		
		$mail_addr_list_ok = "";
		$send_ok_count = 0;
		foreach( $mail_addr_list as $mail_addr ){
			$mail = new PHPMailer(); 
			$mail->IsSMTP(); 
			$mail->CharSet ="utf-8"; 
			$mail->Host = STMPHOST; 
			$mail->SMTPAuth = true; 
			$mail->Username = STMP_USERNAME; 
			$mail->Password = STMP_PASS; 
			$mail->From = STMP_USERNAME.'@'.substr(strstr(STMPHOST , '.') , 1); 
			$mail->FromName = STMP_FROMNAME; 
			$mail->WordWrap = 50;
			$mail->IsHTML(true); 
			$mail->Subject = $subject; 
			$mail->Body = addslashes($content);
			$mail->AddBCC($mail_addr);
			if( $mail->Send() ) {
				$send_ok_count++;
				if ( $mail_addr_list_ok == "" ) {
					$mail_addr_list_ok = $mail_addr;
				} else {
					$mail_addr_list_ok .= ",".$mail_addr;
				}
			} 
		}
		
		if( $send_ok_count < 1 ) { 
			$msg = "发送失败！请检查stmp服务器配置是否正确<br/>Mailer Error: " . $mail->ErrorInfo;
			message($msg);
		} else {
			$this->addMailData( $subject , $content, $mail_addr_list_ok );
			message( '成功发送了'.$send_ok_count.'封邮件<br/>', '?q=sendMail' );
		}
    }

	//商家 菜色列表
	public function mealType(){
		$t = $this->smartyObject();
		$t->assign('maxType' , $this->mealTypeList(0));
		$t->assign('minType' , $this->mealTypeList(1));
		$t->display("#mealType.html");
	}
	
	public function todayMenu(){
		$list = $this->currentMenu();
		$dataList = $this->distinctMealType();
		$mrr = array();
		$urr = array();
		$irr = array();
		foreach ($list as $t){
			array_push($mrr , $t['mealId']);
			array_push($urr , $t['userId']);
			array_push($irr ,  $t['id']);
		}
		$t = $this->smartyObject();
		$t->assign('list' ,$list );	
	    $t->assign('mealKey' , join($mrr , ','));	
	    $t->assign('userKey' , join($urr , ','));	
	    $t->assign('idKey' , join($irr , ','));
	    $t->assign('parentName' , $dataList[0]);
	    $t->assign('parentKey' , $dataList[1]);
		$t->assign('user',$this->viewDepartmentUserList(00));
		$t->assign('select' , $this->makeSelectList());
		$t->display("#todayMenu.html");
	}
	
	private function makeSelectList(){
		$max = $this->mealTypeList(0);
		$min = $this->mealTypeList(1);
		$str = '<select name="mealId" onchange="toType(this)"><option>请选择</option>';
		foreach ($max as &$t){
			$str .= '<optgroup label="'.$t['name'].'" />';
			foreach ($min as &$c){
				if ($c['parentId'] != $t['id']) {
					continue;
				}
				$str .= '<option value="'.$c['id'].'">'.$c['name'].'</option>';
			}
		}
		$str .='</select>';
		return $str;
	}

    
}

?>